const shelljs = require("shelljs");
const path = require("path");
const fs = require("fs");

/**
 * 
 * @param {*} name 项目名称
 * @param {*} branch 分支
 * @param {*} gitHttpUrl 项目的git完整地址
 */
const handleProject = (name, branch, gitHttpUrl) => {
  // 项目根路径
  const BasePath = path.join(process.cwd(), "../../projects");
  const project_path = path.join(BasePath, name);
  if (!fs.existsSync(project_path)) {
    console.log("项目不存在,开始创建项目", name);
    shelljs.exec(`git clone -b ${branch} ${gitHttpUrl}`, {
      encoding: "utf-8",
    });
    shelljs.cd(project_path);
    shelljs.exec(`git pull`, {
      encoding: "utf-8",
    });
  } else {
    console.log("项目存在，切换分支", branch);
    shelljs.cd(project_path);
    shelljs.exec(`git fetch`, {
      encoding: "utf-8",
    });
    shelljs.exec(`git checkout ${branch}`, {
      encoding: "utf-8",
    });
    shelljs.exec(`git pull`, {
      encoding: "utf-8",
    });
  }
  // shelljs.exec(`cnpm install`, {
  //   encoding: "utf-8",
  // });
  // shelljs.exec(`npm run build`, {
  //   encoding: "utf-8",
  // });
  console.log('结束>>>');
};

exports.handleProject = handleProject;